#include "rewine/MapIndicator.hpp"

namespace rewine {

MapIndicator::MapIndicator(const std::shared_ptr<int> &pfd, const void *start, size_t offset, size_t size)
        : pfd(pfd), start(start), offset(offset), size(size), pos(0), carry(0) {
}

void *
MapIndicator::front() const {
    return (void *)this->start;
}

void *
MapIndicator::back() const {
    return (char *)this->start + this->size;
}

void *
MapIndicator::current() const {
    return (char *)this->start + this->pos;
}

void *
MapIndicator::jump(size_t _pos, size_t _carry) {
    if (_pos + _carry >= this->size) return nullptr;
    this->pos = _pos;
    this->carry = _carry;
    return this->current();
}

void *
MapIndicator::move(size_t _offset, size_t _carry) {
    return jump(this->pos + _offset, _carry);
}

void *
MapIndicator::next(size_t _carry) {
    return move(this->carry, _carry);
}

void *
MapIndicator::rewind() {
    return jump(0, 0);
}

} // namespace rewine
